<?php

Yii::import('common.models._base.BasePurchaseOrder');

class PurchaseOrder extends BasePurchaseOrder {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static function getUsersName($userId) {
        //echo $userId;die;
        $userDetails = Users::getFrontEndUser($userId);

        return (!empty($userDetails->first_name)) ? $userDetails->first_name . ' ' . $userDetails->last_name : $userDetails->user->username;
    }

    public static function label($n = 1) {
        return Yii::t('app', 'Purchase Order|Purchase Orders', $n);
    }

    public function search() {
        $criteria = new CDbCriteria;
        $criteria->compare('id', $this->id);
        $criteria->compare('users_id', $this->users_id);
        $criteria->compare('total_price', $this->total_price, true);
        $criteria->compare('order_date_time', $this->order_date_time, true);
        $criteria->compare('order_confirmed', $this->order_confirmed);
        $criteria->compare('coupons_id', $this->coupons_id);
        $criteria->compare('transaction_id', $this->transaction_id, true);
        $criteria->order = 'order_date_time DESC';

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function customeSearch() {       
        $criteria = new CDbCriteria;
        $criteria->compare('id', $this->id);
        $criteria->compare('users_id', $this->users_id);
        $criteria->compare('total_price', $this->total_price, true);
        $criteria->compare('order_date_time', $this->order_date_time, true);
        $criteria->compare('order_confirmed', $this->order_confirmed);
        $criteria->compare('coupons_id', $this->coupons_id);
        $criteria->compare('transaction_id', $this->transaction_id, true);
        $criteria->order = 'order_date_time DESC';
        if (isset($_GET['from_date'])) {
            $criteria->addCondition('LEFT(order_date_time,10)>="' . $_GET['from_date'] . '" AND LEFT(order_date_time,10)<="' . $_GET['to_date'] . '" ');
        }
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

}